package com.tbit.uqbike.webmanager.controller;

import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.dto.CardAccountBoundAddDTO;
import com.tbit.uqbike.object.pojo.dto.CardAccountBoundDTO;
import com.tbit.uqbike.object.pojo.vo.CardAccountBoundVO;
import com.tbit.uqbike.object.pojo.vo.CardAccountIdAndNameVO;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.CardAccountBoundService;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 区域共享配置
 *
 * @Author:BUGTian
 * @DATE: 2023/1/5
 */
@RestController
@RequestMapping("/cardAccountBound")
public class CardAccountBoundController {

    @Autowired
    private CardAccountBoundService cardAccountBoundService;


    /**
     * 查询区域共享配置
     *
     * @param brandId   品牌id（不传查全部）
     * @param accountId 区域id （不传查全部）
     * @param cardType  卡券类型 2：骑行卡 （不传查全部）
     * @return
     */
    @GetMapping("/queryList")
    public Result<List<CardAccountBoundVO>> queryList(Integer brandId, Integer accountId, Integer cardType) {
        return Result.success(cardAccountBoundService.queryList(brandId, accountId, cardType));
    }


    /**
     * 新增区域共享配置
     *
     * @param cardAccountBoundAddDTO
     * @return
     */
    @PostMapping("/addConfig")
    @OperateLog(module = ModuleEnum.CARD_ACCOUNT_BOUND, methods = MethodEnum.CARD_ACCOUNT_BOUND_ADD_CONFIG)
    public Result<String> addConfig(@RequestBody CardAccountBoundAddDTO cardAccountBoundAddDTO) {
        Assert.notNulls("参数有误", cardAccountBoundAddDTO.getBrandId(), cardAccountBoundAddDTO.getConfigName(), cardAccountBoundAddDTO.getCardType());
        if (cardAccountBoundAddDTO.getConfigName().length() > 255){
            return Result.error("配置名称过长");
        }
        return cardAccountBoundService.addConfig(cardAccountBoundAddDTO.getBrandId(), cardAccountBoundAddDTO.getConfigName(), cardAccountBoundAddDTO.getCardType());
    }

    /**
     * 修改区域共享配置名称
     *
     * @param id         主键id
     * @param configName 配置名称
     * @return
     */
    @PutMapping("/updateConfig")
    @OperateLog(module = ModuleEnum.CARD_ACCOUNT_BOUND, methods = MethodEnum.CARD_ACCOUNT_BOUND_UPDATE_CONFIG)
    public Result<String> updateConfig(@NotNull Long id, @NotNull String configName) {
        Assert.notNulls("参数有误", id, configName);
        return cardAccountBoundService.updateConfig(id, configName);
    }

    /**
     * 绑定区域检查
     *
     * @param cardAccountBoundDTO
     * @return 已绑定区域名称列表
     */
    @PostMapping("/boundCheck")
    public Result<List<String>> boundCheck(@RequestBody CardAccountBoundDTO cardAccountBoundDTO) {
        Assert.notNulls("参数有误", cardAccountBoundDTO.getAccountIds(), cardAccountBoundDTO.getId());
        return cardAccountBoundService.boundCheck(cardAccountBoundDTO);
    }

    /**
     * 新增绑定区域
     *
     * @param cardAccountBoundDTO
     * @return
     */
    @PostMapping("/bound")
    @OperateLog(module = ModuleEnum.CARD_ACCOUNT_BOUND, methods = MethodEnum.CARD_ACCOUNT_BOUND_BOUND)
    public Result<String> bound(@RequestBody CardAccountBoundDTO cardAccountBoundDTO) {
        Assert.notNulls("参数有误", cardAccountBoundDTO.getAccountIds(), cardAccountBoundDTO.getId());
        return cardAccountBoundService.bound(cardAccountBoundDTO);
    }

    /**
     * 删除区域共享配置
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("/del")
    @OperateLog(module = ModuleEnum.CARD_ACCOUNT_BOUND, methods = MethodEnum.CARD_ACCOUNT_BOUND_DEL)
    public Result<String> del(@NotNull Long id) {
        cardAccountBoundService.removeById(id);
        return Result.success();
    }

    /**
     * 查询配置绑定的区域id和名称列表
     *
     * @param id 主键
     * @return
     */
    @GetMapping("/getCardAccountIdAndName")
    public Result<List<CardAccountIdAndNameVO>> getCardAccountIdAndName(@NotNull Long id) {
        return cardAccountBoundService.getCardAccountIdAndName(id);
    }

    /**
     * 删除单个区域的绑定
     *
     * @param id        主键id
     * @param accountId 删除的区域
     * @return
     */
    @DeleteMapping("/delOneAccountBound")
    @OperateLog(module = ModuleEnum.CARD_ACCOUNT_BOUND, methods = MethodEnum.CARD_ACCOUNT_BOUND_DEL_ONE_ACCOUNT_BOUND)
    public Result<String> delOneAccountBound(@NotNull Long id, @NotNull Integer accountId) {
        return cardAccountBoundService.delOneAccountBound(id, accountId);
    }


}
